package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.egg.ylt.R;
import com.egg.ylt.pojo.CancelAppoinEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/9/4.
 */

public class ADA_CancelList extends RecyclerView.Adapter<ADA_CancelList.CancelViewHolder> {


    private Context mContext;
    private LayoutInflater mInflater;
    private List<CancelAppoinEntity> mList;
    private OnItemClickListener itemClickListener;
    public int isSelected = -15;

    public ADA_CancelList(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    //添加数据
    public void addList(List<CancelAppoinEntity> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public List<CancelAppoinEntity> getListData(){
        return mList;
    }

    @Override
    public CancelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CancelViewHolder(mInflater.inflate(R.layout.item_cancel_list, parent, false));
    }

    @Override
    public void onBindViewHolder(CancelViewHolder holder, final int position) {
        final CancelAppoinEntity entity = mList.get(position);
        if (isSelected == position){
            entity.setSure(true);
        }else{
            entity.setSure(false);
        }
        if (entity.isSure()){
            holder.ivSelectorCancel.setSelected(true);
        }else{
            holder.ivSelectorCancel.setSelected(false);
        }
        holder.tvContextCancel.setText(entity.getName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSelected = position;
                itemClickListener.onClick(entity.getName());
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CancelViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_selector_cancel)
        ImageView ivSelectorCancel;
        @BindView(R.id.tv_context_cancel)
        TextView tvContextCancel;

        public CancelViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(String text);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
